﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using GdipEffect;

namespace EditImage
{
    public partial class frmMain : Form
    {
        
        public frmMain()
        {
            InitializeComponent();
        }

        string fileName = string.Empty;

        private void 打开图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "支持的图像文件|*.bmp;*.jpg;*.png";
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                fileName = ofd.FileName;
                pictureBox1.Image = new Bitmap(fileName);
            }
        }

        private void 灰度化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileName == string.Empty)
            {
                MessageBox.Show("请先打开图像", "Error", MessageBoxButtons.OK);
                return;
            }
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Bitmap img = new Bitmap(pictureBox1.Image);
            Bitmap transImg = ConvertToGrayscale(img);
            pictureBox1.Image = transImg;
            sw.Stop();
            timeUse.Text = sw.ElapsedMilliseconds.ToString() + " ms";

        }

        public Bitmap ConvertToGrayscale(Bitmap source)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);

            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);
                    int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                    bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
                }
            }
            return bm;
        }
        public Bitmap InverseColor(Bitmap source)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);
            for (int y = 0; y < bm.Height; y++)
            {

                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);
                    Color tc = Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B);
                    bm.SetPixel(x, y, tc);
                }
            }
            return bm;

        }
        private void 提取线稿ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("开发中...");
        }
        private void 反色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileName == string.Empty)
            {
                MessageBox.Show("请先打开图像", "Error", MessageBoxButtons.OK);
                return;
            }
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Bitmap img = new Bitmap(pictureBox1.Image);
            Bitmap transImg = InverseColor(img);
            pictureBox1.Image = transImg;
            sw.Stop();
            timeUse.Text = sw.ElapsedMilliseconds.ToString() + " ms";
        }
        private void 高斯模糊ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmGaussianBlurConfig frmConfig = new frmGaussianBlurConfig();
            frmConfig.ShowDialog();
            //Effect.GaussianBlur(pictureBox1.Image);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }
    }
}